﻿#region using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Timers;
using System.Collections;
using Microsoft.VisualBasic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace BrainPong
{
    public class Ball
    {
        
        #region Variable Declaration
        private Vector3 _position;
        private Vector3 _rotation;
        private Vector3 _direction;
        private static Model _model;
        public KeyboardState curKeyState, prevKeyState;
        private float _speed;
        private float  _scale = 0.1f;
        public float radius;
        #endregion


        public Ball(Vector3 position, Vector3 direction, float speed = 1.5f)
        {
            _position = position;
            _direction = direction;
            _rotation = Vector3.Zero;
            radius = _scale * 34;
            _speed = speed;
        }

        public static void loadModel()
        {
            _model = Game1.game.loadModel("ball2");
        }

        public void updatePosition()
        {
            
            if(checkWall())
            {
                _direction.Y = -_direction.Y;
            }
            if (checkBats())
            {
                _direction.X = -_direction.X;
            }
            else if (checkHoleLeft())
            {
                _position = Vector3.Zero;                      //balletje terug op startpunt zetten.
                Game1.game.bat2.points++;
            }
            else if (checkHoleRight())
            {
                _position = Vector3.Zero;                      //balletje terug op startpunt zetten.
                Game1.game.bat1.points++;
            }

            _position += _direction * _speed;

        }


        private Boolean checkBats()
        {
            float xmaxBat2 = Game1.game.bat2.getPosition().X + Game1.game.bat2.width / 2;
            float xminBat2 = Game1.game.bat2.getPosition().X - Game1.game.bat2.width / 2;
            float xmaxBat1 = Game1.game.bat1.getPosition().X + Game1.game.bat1.width / 2;
            float xminBat1 = Game1.game.bat1.getPosition().X - Game1.game.bat1.width / 2;

            float xmaxBall = (_position + _direction * _speed).X + radius;
            float xminBall = (_position + _direction * _speed).X - radius;

            float ymaxBat2 = Game1.game.bat2.getPosition().Y + Game1.game.bat2.height / 2;
            float yminBat2 = Game1.game.bat2.getPosition().Y - Game1.game.bat2.height / 2;
            float ymaxBat1 = Game1.game.bat1.getPosition().Y + Game1.game.bat1.height / 2;
            float yminBat1 = Game1.game.bat1.getPosition().Y - Game1.game.bat1.height / 2;

            float ymaxBall = (_position + _direction * _speed).Y + radius;
            float yminBall = (_position + _direction * _speed).Y - radius;

            if(xmaxBall > xminBat2 && xmaxBall < xmaxBat2)
                if(yminBall < ymaxBat2 && ymaxBall > yminBat2)
                    return true;
            if (xminBall > xminBat1 && xminBall < xmaxBat1)
                if (yminBall < ymaxBat1 && ymaxBall > yminBat1)
                    return true;
            return false;
        }


        private Boolean checkWall()
        {
            
            float ymax = Game1.veld.YMax();
            float ymin = Game1.veld.YMin();

            float yMax = (_position + _direction * _speed).Y;
            float yMin = (_position + _direction * _speed).Y;

            Boolean YMAX = yMax >= ymax;
            Boolean YMIN = yMin <= ymin;

            return (YMAX || YMIN);
        }

        private Boolean checkHoleLeft()
        {
            float xmin = Game1.veld.XMin();

            float xMin = (_position + _direction * _speed).X;

            Boolean XMIN = xMin > xmin;

            return (XMIN);
        }


        private Boolean checkHoleRight()
        {
            float xmax = Game1.veld.XMax();
            

            float xMax = (_position + _direction * _speed).X;
            

            Boolean XMAX = xMax < xmax;
            

            return (XMAX);
        }



        public void draw(Game1 game)
        {
            game.drawModel(_model, _position, _rotation, _scale);
        }
    }
}
